package com.nulldev.util.internal.backport.httpclient_rw.backports;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;

import com.nulldev.util.internal.backport.concurrency9.Lists;

public class ProxySelectors {
	/**
	 * Returns a ProxySelector which uses the given proxy address for all HTTP and
	 * HTTPS requests. If proxy is {@code null} then proxying is disabled.
	 *
	 * @param proxyAddress The address of the proxy
	 *
	 * @return a ProxySelector
	 *
	 * @since 9
	 */
	public static ProxySelector of(InetSocketAddress proxyAddress) {
		return new StaticProxySelector(proxyAddress);
	}

	static class StaticProxySelector extends ProxySelector {
		private static final List<Proxy> NO_PROXY_LIST = Lists.of(Proxy.NO_PROXY);
		final List<Proxy> list;

		StaticProxySelector(InetSocketAddress address) {
			Proxy p;
			if (address == null) {
				p = Proxy.NO_PROXY;
			} else {
				p = new Proxy(Proxy.Type.HTTP, address);
			}
			list = Lists.of(p);
		}

		@Override
		public void connectFailed(URI uri, SocketAddress sa, IOException e) {
			/* ignore */
		}

		@Override
		public synchronized List<Proxy> select(URI uri) {
			String scheme = uri.getScheme().toLowerCase();
			if (scheme.equals("http") || scheme.equals("https")) {
				return list;
			} else {
				return NO_PROXY_LIST;
			}
		}
	}
}
